package com.thread.sample;

import java.io.FileOutputStream;
import java.io.FileWriter;
import java.util.Date;
import java.util.concurrent.Callable;

public class FileCeatorCallable implements Callable<Result> {

	private int LINE_NUMBER = 1000000;
	private String fileName;
	private String keyword;
	
	public FileCeatorCallable(String fileName, String keyword) {
		this.keyword = keyword;
		this.fileName = fileName;
	}
	
	@Override
	public Result call() throws Exception {

		System.out.println("Thread " + Thread.currentThread().getName() + " is working...");
			
//		FileWriter writer = new FileWriter(fileName, true);
		FileOutputStream out = new FileOutputStream(fileName);
		
		int index = 0;
		while ( index < LINE_NUMBER){
			long l = (new Date()).getTime();
			if (l % 10 != 0)
				continue;
			
			index ++;
			
			String line = String.valueOf((new Date()).getTime()) 
					+ keyword + Math.random() + "\r\n";
			
			out.write(line.getBytes());
//			writer.write(line);
			
		}
		out.flush();
		out.close();
//		writer.close();
		
		String reason = "Name:" + fileName + " with " + keyword;
		Result result = new Result();
		result.setReason(reason);
		
		System.out.println("Thread " + Thread.currentThread().getName() + "  finished.");
		return result;
	}

}
